﻿using Helper;
using IssueTracker.CustomControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ViewModel;

namespace IssueTracker
{
    public partial class MainWindow : Form
    {
        public int _id;
        public Member currentUser { get; set; }
        public StickyForm objStickyForm;
        public issueList iList;

        public MainWindow()
        {
            InitializeComponent();

        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            lblUser.Text = ActiveSession.User.UserName;
            lblVersion.Text = string.Format("v {0}", Application.ProductVersion);
            lblAboutVersion.Text = lblVersion.Text;
            this.ResizeRedraw = true;
            this.Refresh();

            if (ActiveSession.User.Role != Role.Admininstrator)
            {
                adminToolStripMenuItem.Visible = false;
                createNewtoolStripMenuItem.Visible = false;
            }

            //set color code to lable
            lblUrgent.BackColor = System.Drawing.ColorTranslator.FromHtml(Priority.Urgent.GetColor());//Color.Red;
            lblHigh.BackColor = System.Drawing.ColorTranslator.FromHtml(Priority.High.GetColor());//Color.Orange;
            lblMedium.BackColor = System.Drawing.ColorTranslator.FromHtml(Priority.Medium.GetColor());//Color.SteelBlue;
            //lblLow.BackColor = System.Drawing.ColorTranslator.FromHtml(Priority.Low.GetColor());//Color.White;

        }

        #region background worker

        //private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        //{
        //    Random ran = new Random();
        //    while (!backgroundWorker1.CancellationPending)
        //    {
        //        Thread.Sleep(2000);
        //        backgroundWorker1.ReportProgress(0, ran.Next(100));
        //    }
        //}

        //private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        //{
        //    //lblNewIssues.Text = string.Format("{0}", e.Result);
        //}

        //private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        //{
        //    lblNewIssues.Text = string.Format("{0}", e.UserState);
        //}
        #endregion

        private void MainWindow_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            objStickyForm.objMainWindow = null;
        }

        private void MainWindow_Resize(object sender, EventArgs e)
        {
            this.Update();
        }

        #region main menu
        private void SelectMenu(string menuItemName)
        {
            //adminToolStripMenuItem.Font = new Font(adminToolStripMenuItem.Font, FontStyle.Regular);
            //issuesToolStripMenuItem.Font = new Font(issuesToolStripMenuItem.Font, FontStyle.Regular);
            //createNewtoolStripMenuItem.Font = new Font(createNewtoolStripMenuItem.Font, FontStyle.Regular);
            //reportsStripMenuItem.Font = new Font(reportsStripMenuItem.Font, FontStyle.Regular);
            //changePasswordToolStripMenuItem.Font = new Font(changePasswordToolStripMenuItem.Font, FontStyle.Regular);
            //logOutToolStripMenuItem.Font = new Font(logOutToolStripMenuItem.Font, FontStyle.Regular);


            adminToolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuNormalBgColor);
            issuesToolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuNormalBgColor);
            createNewtoolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuNormalBgColor);
            reportsStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuNormalBgColor);
            changePasswordToolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuNormalBgColor);
            logOutToolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuNormalBgColor);

            //adminToolStripMenuItem.Image = IssueTracker.Properties.Resources.admin0;
            //issuesToolStripMenuItem.Image = IssueTracker.Properties.Resources.tasks0;
            //createNewtoolStripMenuItem.Image = IssueTracker.Properties.Resources.create_new0;
            //reportsStripMenuItem.Image = IssueTracker.Properties.Resources.report0;
            //changePasswordToolStripMenuItem.Image = IssueTracker.Properties.Resources.psw0;
            //logOutToolStripMenuItem.Image = IssueTracker.Properties.Resources.logout0;
            switch (menuItemName)
            {
                case "adminToolStripMenuItem":
                    adminToolStripMenuItem.Image = IssueTracker.Properties.Resources.admin;
                    //adminToolStripMenuItem.Font = new Font(adminToolStripMenuItem.Font, FontStyle.Bold);
                    adminToolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuActiveBgColor);
                    break;
                case "tasksToolStripMenuItem":
                    issuesToolStripMenuItem.Image = IssueTracker.Properties.Resources.tasks;
                    //issuesToolStripMenuItem.Font = new Font(issuesToolStripMenuItem.Font, FontStyle.Bold);
                    issuesToolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuActiveBgColor);
                    break;
                case "createNewtoolStripMenuItem":
                    createNewtoolStripMenuItem.Image = IssueTracker.Properties.Resources.create_new;
                    //createNewtoolStripMenuItem.Font = new Font(createNewtoolStripMenuItem.Font, FontStyle.Bold);
                    createNewtoolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuActiveBgColor);
                    break;
                case "reportsStripMenuItem":
                    reportsStripMenuItem.Image = IssueTracker.Properties.Resources.report;
                    //reportsStripMenuItem.Font = new Font(reportsStripMenuItem.Font, FontStyle.Bold);
                    reportsStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuActiveBgColor);
                    break;
                case "changePasswordToolStripMenuItem":
                    changePasswordToolStripMenuItem.Image = IssueTracker.Properties.Resources.psw;
                    //changePasswordToolStripMenuItem.Font = new Font(changePasswordToolStripMenuItem.Font, FontStyle.Bold);
                    changePasswordToolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuActiveBgColor);
                    break;
                case "logOutToolStripMenuItem":
                    logOutToolStripMenuItem.Image = IssueTracker.Properties.Resources.logout;
                    //logOutToolStripMenuItem.Font = new Font(logOutToolStripMenuItem.Font, FontStyle.Bold);
                    logOutToolStripMenuItem.BackColor = Color.FromName(AppTheme.MainMenuActiveBgColor);
                    break;
                default:
                    break;
            }
        }

        private void adminToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            SelectMenu("adminToolStripMenuItem");
            panelMain.Controls.Clear();
            admin obj = new admin();
            obj.Dock = DockStyle.Fill;
            panelMain.Controls.Add(obj);
            Cursor.Current = Cursors.Default;
        }

        private void tasksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadIssueList(true);
        }

        public void LoadIssueList(bool refresh)
        {
            Cursor.Current = Cursors.WaitCursor;
            SelectMenu("tasksToolStripMenuItem");
            panelMain.Controls.Clear();
            if (refresh)
            {
                iList = new issueList();
                iList.objMain = this;
                iList.Dock = DockStyle.Fill;
            }
            panelMain.Controls.Add(iList);
            Cursor.Current = Cursors.Default;
        }

        private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelectMenu("changePasswordToolStripMenuItem");
            panelMain.Controls.Clear();
            change_password obj = new change_password();
            obj.Dock = DockStyle.Fill;
            panelMain.Controls.Add(obj);
        }

        private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dialog = MessageBox.Show("You are going to log out from the system. Are you sure?", "Please conform?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialog == DialogResult.Yes)
            {
                IssueTracker.Properties.Settings.Default.username = string.Empty;
                IssueTracker.Properties.Settings.Default.password = string.Empty;
                IssueTracker.Properties.Settings.Default.Save();
                objStickyForm.StopPendingBackgroundOperation();
                objStickyForm.LoadLoginPanel();
                this.Close();
                objStickyForm.ShowLoginWindow();
            }
        }

        #endregion

        private void createNewtoolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            SelectMenu("createNewtoolStripMenuItem");
            panelMain.Controls.Clear();
            newIssue obj = new newIssue();
            obj.Dock = DockStyle.Fill;
            panelMain.Controls.Add(obj);
            Cursor.Current = Cursors.Default;
        }

        private void reportsStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            SelectMenu("reportsStripMenuItem");
            panelMain.Controls.Clear();
            reports obj = new reports();
            obj.Dock = DockStyle.Fill;
            panelMain.Controls.Add(obj);
            Cursor.Current = Cursors.Default;
        }

        public void LoadIssueDetails(Issue obj)
        {
            Cursor.Current = Cursors.WaitCursor;
            issueDetails issue = new issueDetails(obj);
            issue.objMain = this;
            issue.Dock = DockStyle.Fill;

            panelMain.Controls.Clear();
            panelMain.Controls.Add(issue);
            Cursor.Current = Cursors.Default;
        }
    }
}
